﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Threading;
using System.IO;
using System.Data.SqlClient;

namespace statue
{
    class Program
    {
        private static string tblfminfo = "tblfminfo1";
        private static string tbluserinfo = "tbluserinfo1";
        private static string link = "bw_orc";
        //private static string link = "mysql_link";
        static void Main(string[] args)
        {
            while (true)
            {
                //获取需要进行计算的数据条数
                int num = 0;
                try
                {
                    num = (int)SqlHelper.ExecuteScalar(SqlHelper.conStrSource, "SELECT count(0)  FROM OPENQUERY("+link+",'select * from BW_ZJB_SBXX_CZRZ where FD_XBISDKJ <> ''y'' or FD_XBISDKJ is null')");
                }
                catch (Exception e)
                {
                    WriteLog("从日志表获取需要计算的数据条数出现异常");
                    WriteLog(e.ToString());
                }

                if (num > 0)//需要更新数据
                {
                    //获取需要处理的数据
                    string sql = "SELECT *  FROM OPENQUERY(" + link + ",'select FD_RZSEQ,FD_XBBH,FD_JBBH,FD_CZ,FD_DATE from BW_ZJB_SBXX_CZRZ where (FD_XBISDKJ <> ''y'' or FD_XBISDKJ is null)')";
                    List<BW_ZJB_SBXX_CZRZ> logList = new List<BW_ZJB_SBXX_CZRZ>();
                    try
                    {
                        using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.conStrSource, sql))
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    BW_ZJB_SBXX_CZRZ log = new BW_ZJB_SBXX_CZRZ();
                                    log.FD_RZSEQ = Convert.ToInt32(reader.GetString(0));
                                    log.newFmaddress = reader.IsDBNull(1) ? "" : reader.GetString(1);
                                    log.oldFmaddress = reader.IsDBNull(2) ? "" : reader.GetString(2);
                                    log.action = reader.IsDBNull(3) ? "" : reader.GetString(3);
                                    log.date = reader.GetDateTime(4);
                                    logList.Add(log);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        WriteLog("从日志表获取获取需要处理的数据出现异常");
                        WriteLog(e.ToString());
                    }
                    //处理日志
                    calculate(logList);
                }
                #region 休眠一段时间
                int SleepTime = int.Parse(ConfigurationManager.AppSettings["SleepTime"]);
                Thread.Sleep(SleepTime);
                #endregion
            }
        }

        //处理日志
        private static void calculate(List<BW_ZJB_SBXX_CZRZ> logList)
        {
            for (int i = 0; i < logList.Count; i++)
            {
                calculateOneLog(logList[i]);
            }
        }

        //处理一条日志
        private static void calculateOneLog(BW_ZJB_SBXX_CZRZ bW_ZJB_SBXX_CZRZ)
        {
            switch (bW_ZJB_SBXX_CZRZ.action)
            {
                case "01":
                    //如果没有这块表
                    if ((int)SqlHelper.ExecuteScalar(SqlHelper.conStrTarget, "select count(0) from "+tblfminfo+" where fmaddress=" + bW_ZJB_SBXX_CZRZ.newFmaddress) < 1)
                    {
                        //添加我们库日志
                        UpdateLog log = new UpdateLog();
                        log.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                        log.fmaddress = bW_ZJB_SBXX_CZRZ.newFmaddress;
                        log.type = "新表入库";
                        log.remark = "仪表编号为" + bW_ZJB_SBXX_CZRZ.newFmaddress + ",缺少基本信息";
                        updateLog(log);
                    }
                    //更新日志中间表
                    UpdateLogMiddle logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                     logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    //同步sbxx
                    GetSbxx(bW_ZJB_SBXX_CZRZ.newFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;
                case "02":
                    //如果有这块表
                    if ((int)SqlHelper.ExecuteScalar(SqlHelper.conStrTarget, "select count(0) from "+tblfminfo+" where fmaddress=" + bW_ZJB_SBXX_CZRZ.newFmaddress) > 0)
                    {
                        //删除表信息
                        SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "delete "+tblfminfo+" where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'");
                        //添加我们库日志
                        UpdateLog log = new UpdateLog();
                        log.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                        log.fmaddress = bW_ZJB_SBXX_CZRZ.newFmaddress;
                        log.type = "新表入库删除";
                        log.remark = "删除仪表编号为" + bW_ZJB_SBXX_CZRZ.newFmaddress + "的基本信息";
                        log.sql = "delete " + tblfminfo + " where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'";
                        
                        updateLog(log);
                    }
                    //更新日志中间表
                    logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                     logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.newFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;

                case "03":

                    //更新水表状态为“出库”
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set statue='出库' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'");
                    //添加我们库日志
                    UpdateLog log1 = new UpdateLog();
                    log1.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    log1.fmaddress = bW_ZJB_SBXX_CZRZ.newFmaddress;
                    log1.type = "新表出库";
                    log1.remark = "仪表编号为" + bW_ZJB_SBXX_CZRZ.newFmaddress + ",水表出库";
                    log1.sql = "update " + tblfminfo + " set statue='出库' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'";
                    updateLog(log1);

                    //更新日志中间表
                    logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                     logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.newFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;
                case "04":

                    //更新水表状态为“在库”
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set statue='在库' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'");
                    //添加我们库日志
                    UpdateLog log2 = new UpdateLog();
                    log2.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    log2.fmaddress = bW_ZJB_SBXX_CZRZ.newFmaddress;
                    log2.type = "新表回库";
                    log2.remark = "仪表编号为" + bW_ZJB_SBXX_CZRZ.newFmaddress + ",水表出库删除";
                    log2.sql = "update " + tblfminfo + " set statue='在库' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'";
                    updateLog(log2);

                    //更新日志中间表
                    logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                     logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.newFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;
                case "05":
                    //更新水表状态为“已安装”
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set statue='已安装' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'");                    
                    //添加我们库日志
                    UpdateLog log4 = new UpdateLog();
                    log4.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    log4.fmaddress = bW_ZJB_SBXX_CZRZ.newFmaddress;
                    log4.type = "新表安装";
                    log4.remark = "仪表编号为" + bW_ZJB_SBXX_CZRZ.newFmaddress + ",水表安装";
                    log4.sql = "update " + tblfminfo + " set statue='已安装' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'";
                    updateLog(log4);

                    //更新日志中间表
                    logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                     logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.newFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;
                case "水表安装删除":
                    //更新水表状态为“在库”
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set statue='领表',userid='',username='' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'");
                    //添加我们库日志
                    UpdateLog log5 = new UpdateLog();
                    log5.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    log5.fmaddress = bW_ZJB_SBXX_CZRZ.newFmaddress;
                    log5.type = "水表安装删除";
                    log5.remark = "仪表编号为" + bW_ZJB_SBXX_CZRZ.newFmaddress + ",水表安装删除";
                    log5.sql = "update " + tblfminfo + " set statue='领表',userid='',username='' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'";
                    updateLog(log5);

                    //更新日志中间表
                    logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                     logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.newFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;
                case "06":
                    //获取安装信息
                    sbxx sb = new sbxx();
                    using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.conStrSource, "SELECT *  FROM OPENQUERY(" + link + ",'select FD_JSDBH,FD_YHMC from BW_ZJB_SBXX where FD_SBBH=''" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "''')"))
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {
                                sb.userid = reader.IsDBNull(0) ? "" : reader.GetString(0);
                                sb.username = reader.IsDBNull(1) ? "" : reader.GetString(1);
                            }
                        }
                    }
                    //更新水表状态为“已安装”
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set statue='已安装',userid='" + sb.userid + "',username='" + sb.username + "' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'");

                    //添加我们库日志
                    UpdateLog log6 = new UpdateLog();
                    log6.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    log6.fmaddress = bW_ZJB_SBXX_CZRZ.newFmaddress;
                    log6.type = "机械换远传";
                    log6.remark = "仪表编号为" + bW_ZJB_SBXX_CZRZ.newFmaddress + ",机械换远传";
                    log6.sql = "update " + tblfminfo + " set statue='已安装',userid='" + sb.userid + "',username='" + sb.username + "' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'";
                    updateLog(log6);


                    //更新日志中间表
                    logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.newFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;
                case "07":

                    //更旧水表状态为“旧表入库”
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set statue='旧表入库',userid='',username='' where fmaddress='" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "'");
                    //获取安装信息
                    sb = new sbxx();
                    using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.conStrSource, "SELECT *  FROM OPENQUERY(" + link + ",'select FD_JSDBH,FD_YHMC from BW_ZJB_SBXX where FD_SBBH=''" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "''')"))
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {
                                sb.userid = reader.IsDBNull(0) ? "" : reader.GetString(0);
                                sb.username = reader.IsDBNull(1) ? "" : reader.GetString(1);
                            }
                        }
                    }
                    //修改用户状态
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tbluserinfo+" set statue='已销户' where userid='" + sb.userid + "'");
                    //添加我们库日志
                    UpdateLog log7 = new UpdateLog();
                    log7.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    log7.fmaddress = bW_ZJB_SBXX_CZRZ.oldFmaddress;
                    log7.type = "远传换机械";
                    log7.remark = "仪表编号为" + bW_ZJB_SBXX_CZRZ.oldFmaddress + ",远传换机械";
                    log7.sql = "update " + tblfminfo + " set statue='旧表入库',userid='',username='' where fmaddress='" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "'" + ";" + "update " + tbluserinfo + " set statue='已销户' where userid='" + sb.userid + "'";
                    updateLog(log7);

                    //更新日志中间表
                    logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.oldFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;
                case "08":
                case "09":
                    //更新旧表状态为“已安装”
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set statue='旧表入库',userid='',username='' where fmaddress='" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "'");
                    //获取安装信息
                    sb = new sbxx();
                    using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.conStrSource, "SELECT *  FROM OPENQUERY(" + link + ",'select FD_JSDBH,FD_YHMC from BW_ZJB_SBXX where FD_SBBH=''" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "''')"))
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {
                                sb.userid = reader.IsDBNull(0) ? "" : reader.GetString(0);
                                sb.username = reader.IsDBNull(1) ? "" : reader.GetString(1);
                            }
                        }
                    }
                    //更新新表状态为“已安装”
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set statue='已安装',userid='" + sb.userid + "',username='" + sb.username + "' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'");

                    //添加我们库日志
                    UpdateLog log8 = new UpdateLog();
                    log8.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    log8.fmaddress = bW_ZJB_SBXX_CZRZ.newFmaddress;
                    log8.type = "远传换远传";
                    log8.remark = "新表编号为" + bW_ZJB_SBXX_CZRZ.newFmaddress + ",旧表编号为" + bW_ZJB_SBXX_CZRZ.oldFmaddress + ",远传换远传";
                    log8.sql = "update " + tblfminfo + " set statue='旧表入库',userid='',username='' where fmaddress='" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "'" + ";" + "update " + tblfminfo + " set statue='已安装',userid='" + sb.userid + "',username='" + sb.username + "' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'";
                    updateLog(log8);

                    //更新日志中间表
                    logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.newFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.oldFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;
                case "10":
                case "11":

                    //获取新表安装信息
                    sb = new sbxx();
                    using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.conStrSource, "SELECT *  FROM OPENQUERY(" + link + ",'select FD_JSDBH,FD_YHMC from BW_ZJB_SBXX where FD_SBBH=''" + bW_ZJB_SBXX_CZRZ.newFmaddress + "''')"))
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {
                                sb.userid = reader.IsDBNull(0) ? "" : reader.GetString(0);
                                sb.username = reader.IsDBNull(1) ? "" : reader.GetString(1);
                            }
                        }
                    }
                    //更新旧表
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set userid='" + sb.userid + "',username='" + sb.username + "' where fmaddress='" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "'");
                    //获取旧表安装信息
                    sb = new sbxx();
                    using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.conStrSource, "SELECT *  FROM OPENQUERY(" + link + ",'select FD_JSDBH,FD_YHMC from BW_ZJB_SBXX where FD_SBBH=" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "')"))
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {
                                sb.userid = reader.GetString(0);
                                sb.username = reader.IsDBNull(1) ? "" : reader.GetString(1);
                            }
                        }
                    }
                    //更新新表
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set userid='" + sb.userid + "',username='" + sb.username + "' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'");

                    //添加我们库日志
                    UpdateLog log9 = new UpdateLog();
                    log9.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    log9.fmaddress = bW_ZJB_SBXX_CZRZ.newFmaddress;
                    log9.type = "表号互换";
                    log9.remark = "新表编号为" + bW_ZJB_SBXX_CZRZ.newFmaddress + ",旧表编号为" + bW_ZJB_SBXX_CZRZ.oldFmaddress + ",表号互换";
                    log9.sql = "update " + tblfminfo + " set userid='" + sb.userid + "',username='" + sb.username + "' where fmaddress='" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "'" + ";" + "update " + tblfminfo + " set userid='" + sb.userid + "',username='" + sb.username + "' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'";
                    updateLog(log9);

                    //更新日志中间表
                    logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.newFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.oldFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;
                case "12":
                    //更新旧表状态为“遗失”
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set statue='遗失',userid='',username='' where fmaddress='" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "'");
                    //获取安装信息
                    sb = new sbxx();
                    using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.conStrSource, "SELECT *  FROM OPENQUERY(" + link + ",'select FD_JSDBH,FD_YHMC from BW_ZJB_SBXX where FD_SBBH=''" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "''')"))
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {
                                sb.userid = reader.IsDBNull(0) ? "" : reader.GetString(0);
                                sb.username = reader.IsDBNull(1) ? "" : reader.GetString(1);
                            }
                        }
                    }
                    //更新新表状态为“已安装”
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set statue='已安装',userid='" + sb.userid + "',username='" + sb.username + "' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'");

                    //添加我们库日志
                    UpdateLog log10 = new UpdateLog();
                    log10.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    log10.fmaddress = bW_ZJB_SBXX_CZRZ.newFmaddress;
                    log10.type = "赔表审核";
                    log10.remark = "新表编号为" + bW_ZJB_SBXX_CZRZ.newFmaddress + ",旧表编号为" + bW_ZJB_SBXX_CZRZ.oldFmaddress + ",赔表审核";
                    log10.sql = "update " + tblfminfo + " set statue='遗失',userid='',username='' where fmaddress='" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "'" + ";" + "update " + tblfminfo + " set statue='已安装',userid='" + sb.userid + "',username='" + sb.username + "' where fmaddress='" + bW_ZJB_SBXX_CZRZ.newFmaddress + "'";
                    updateLog(log10);

                    //更新日志中间表
                    logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.newFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.oldFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;
                case "13":
                    //销户拆表
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set statue='旧表入库',userid='',username='' where fmaddress='" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "'");
                    //获取旧表安装信息
                    sb = new sbxx();
                    using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.conStrSource, "SELECT *  FROM OPENQUERY(" + link + ",'select FD_JSDBH,FD_YHMC from BW_ZJB_SBXX where FD_SBBH=''" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "''')"))
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {
                                sb.userid = reader.IsDBNull(0) ? "" : reader.GetString(0);
                                sb.username = reader.IsDBNull(1) ? "" : reader.GetString(1);
                            }
                        }
                    }
                    //更新用户状态为“已销户”
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tbluserinfo+" set statue='已销户' where userid='" + sb.userid + "'");

                    //添加我们库日志
                    UpdateLog log11 = new UpdateLog();
                    log11.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    log11.fmaddress = bW_ZJB_SBXX_CZRZ.oldFmaddress;
                    log11.type = "销户拆表";
                    log11.remark = "旧表编号为" + bW_ZJB_SBXX_CZRZ.oldFmaddress + ",销户拆表";
                    log11.sql = "update " + tblfminfo + " set statue='旧表入库',userid='',username='' where fmaddress='" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "'" + ";" + "update " + tbluserinfo + " set statue='已销户' where userid='" + sb.userid + "'";
                    updateLog(log11);

                    //更新日志中间表
                    logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.oldFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;
                case "14":
                    //旧表回库
                    SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set statue='旧表入库',userid='',username='' where fmaddress='" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "'");

                    //添加我们库日志
                    UpdateLog log12 = new UpdateLog();
                    log12.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    log12.fmaddress = bW_ZJB_SBXX_CZRZ.oldFmaddress;
                    log12.type = "旧表回库";
                    log12.remark = "旧表编号为" + bW_ZJB_SBXX_CZRZ.oldFmaddress + ",旧表回库";
                    log12.sql = "update " + tblfminfo + " set statue='旧表入库',userid='',username='' where fmaddress='" + bW_ZJB_SBXX_CZRZ.oldFmaddress + "'";
                    updateLog(log12);

                    //更新日志中间表
                    logMiddle = new UpdateLogMiddle();
                    logMiddle.FD_RZSEQ = bW_ZJB_SBXX_CZRZ.FD_RZSEQ;
                    logMiddle.FD_XBISDKJ = "y";
                    logMiddle.FD_XBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    logMiddle.FD_JBISDKJ = "y";
                    logMiddle.FD_JBDATEDKJ = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    updateLogMiddle(logMiddle);
                    GetSbxx(bW_ZJB_SBXX_CZRZ.oldFmaddress, bW_ZJB_SBXX_CZRZ.date);
                    break;
            }
        }

        private static void GetSbxx(string p, DateTime d)
        {
            //获取sbxx
            sbxxall sb = new sbxxall();
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.conStrSource, "SELECT *  FROM OPENQUERY(" + link + ",'select FD_SEQ,FD_SYSDATE,FD_CREATOR,FD_DATE,FD_SBBH,FD_SBKJ,FD_SBLXBH,FD_SBLX,FD_SBCJBH,FD_SBCJ,FD_SBZT,FD_FGS,FD_SQBH,FD_GCMC,FD_YHMC,FD_JSDBH,FD_JSDWZ,FD_SBWZ,FD_SFYXJD,FD_LBR from BW_ZJB_SBXX where FD_SBBH=''" + p + "''')"))
            {
                if (reader.HasRows)
                {
                    if (reader.Read())
                    {
                        sb.FD_SEQ = reader.GetString(0);
                        sb.FD_SYSDATE = reader.GetDateTime(1);
                        sb.FD_CREATOR = reader.GetString(2);
                        sb.FD_DATE = d;
                        sb.FD_SBBH = reader.GetString(4);
                        sb.FD_SBKJ = reader.GetString(5);
                        sb.FD_SBLXBH = reader.GetString(6);
                        sb.FD_SBLX = reader.GetString(7);
                        sb.FD_SBCJBH = reader.GetString(8);
                        sb.FD_SBCJ = reader.GetString(9);
                        sb.FD_SBZT = reader.GetString(10);
                        sb.FD_FGS = reader.IsDBNull(11) ? "" : reader.GetString(11);
                        sb.FD_SQBH = reader.IsDBNull(12) ? "" : reader.GetString(12);
                        sb.FD_GCMC = reader.IsDBNull(13) ? "" : reader.GetString(13);
                        sb.FD_YHMC = reader.IsDBNull(14) ? "" : reader.GetString(14);
                        sb.FD_JSDBH = reader.IsDBNull(15) ? "" : reader.GetString(15);
                        sb.FD_JSDWZ = reader.IsDBNull(16) ? "" : reader.GetString(16);
                        sb.FD_SBWZ = reader.IsDBNull(17) ? "" : reader.GetString(17);
                        sb.FD_SFYXJD = reader.GetString(18);
                        sb.FD_LBR = reader.IsDBNull(19) ? "" : reader.GetString(19);
                    }
                }
            }
            //插入sbxx
            SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "insert into bw_zjb_sbxx (FD_SEQ,FD_SYSDATE,FD_CREATOR,FD_DATE,FD_SBBH,FD_SBKJ,FD_SBLXBH,FD_SBLX,FD_SBCJBH,FD_SBCJ,FD_SBZT,FD_FGS,FD_SQBH,FD_GCMC,FD_YHMC,FD_JSDBH,FD_JSDWZ,FD_SBWZ,FD_SFYXJD,FD_LBR)values('" + sb.FD_SEQ + "','" + sb.FD_SYSDATE + "','" + sb.FD_CREATOR + "','" + sb.FD_DATE + "','" + sb.FD_SBBH + "','" + sb.FD_SBKJ + "','" + sb.FD_SBLXBH + "','" + sb.FD_SBLX + "','" + sb.FD_SBCJBH + "','" + sb.FD_SBCJ + "','" + sb.FD_SBZT + "','" + sb.FD_FGS + "','" + sb.FD_SQBH + "','" + sb.FD_GCMC + "','" + sb.FD_YHMC + "','" + sb.FD_JSDBH + "','" + sb.FD_JSDWZ + "','" + sb.FD_SBWZ + "','" + sb.FD_SFYXJD + "','" + sb.FD_LBR + "')");
            if (!string.IsNullOrEmpty(sb.FD_FGS))
            {
                //更新tblfminfo
                SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tblfminfo+" set orgid=(select orgid from tblCS_org where csnum='" + sb.FD_FGS + "') where fmaddress='" + sb.FD_SBBH + "'");
                //更新tbluserinfo
                SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "update "+tbluserinfo+" set preid=(select orgid from tblCS_org where csnum='" + sb.FD_FGS + "') where userid='" + sb.FD_JSDBH + "'");
            }
        }

        //更新我们日志
        private static void updateLog(UpdateLog log)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.conStrTarget, "insert into tblStatueLog(date,fmaddress,type,remark,sql)values('" + log.date + "','" + log.fmaddress + "','" + log.type + "','" + log.remark + "','" + log.sql + "') ");
            }
            catch (Exception e)
            {
                WriteLog("更新日志出现错误");
                WriteLog(e.ToString());
            }
        }

        //更新日志中间表
        private static void updateLogMiddle(UpdateLogMiddle logMiddle)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.conStrSource, "update OPENQUERY(" + link + ",'select FD_XBISDKJ,FD_XBDATEDKJ,FD_JBISDKJ,FD_JBDATEDKJ  from BW_ZJB_SBXX_CZRZ where FD_RZSEQ=''" + logMiddle.FD_RZSEQ + "''') set FD_XBISDKJ='" + logMiddle.FD_XBISDKJ + "',FD_XBDATEDKJ='" + logMiddle.FD_XBDATEDKJ + "',FD_JBISDKJ='" + logMiddle.FD_JBISDKJ + "',FD_JBDATEDKJ='" + logMiddle.FD_JBDATEDKJ + "'");
            }
            catch (Exception e)
            {
                WriteLog("更新日志中间表出现错误");
                WriteLog(e.ToString());
            }
        }

        /// <summary>
        /// 写日志文件
        /// </summary>
        /// <param name="sMsg"></param>
        public static void WriteLog(string sMsg)
        {
            if (sMsg != "")
            {

                string filename = DateTime.Now.ToString("yyyyMMdd") + "_statue.txt";
                try
                {
                    FileInfo fi = new FileInfo("log\\" + filename);
                    if (!fi.Exists)
                    {
                        using (StreamWriter sw = fi.CreateText())
                        {
                            sw.WriteLine(DateTime.Now + "\n" + sMsg + "\n");
                            sw.Close();
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = fi.AppendText())
                        {
                            sw.WriteLine(DateTime.Now + "\n" + sMsg + "\n");
                            sw.Close();
                        }
                    }
                }
                catch
                {
                }
            }
        }

    }
}
